{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d1048160-b917-485b-8570-e47d27567415",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-06T09:11:29.718769Z",
     "iopub.status.busy": "2022-02-06T09:11:29.718409Z",
     "iopub.status.idle": "2022-02-06T09:11:29.804383Z",
     "shell.execute_reply": "2022-02-06T09:11:29.803825Z",
     "shell.execute_reply.started": "2022-02-06T09:11:29.718708Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8478a8c2-9531-4227-972c-9fdd85677dee",
   "metadata": {},
   "source": [
    "1. reshape和resize都是重新定义数组形状\n",
    "    1. reshape不会修改数组本身，只返回修改后的结果\n",
    "    2. resize会直接修改数组本身\n",
    "2. flatten和ravel都是将数组降为一维数组，且都不会直接修改原数组\n",
    "    1. flatten返回的是原数组的copy，操作返回值不影响原数组\n",
    "    2. ravel返回的是原数组的view，操作返回值会影响原数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f8a83711-bba9-4b3f-9f33-a4781a1caebf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-06T09:11:30.677318Z",
     "iopub.status.busy": "2022-02-06T09:11:30.677083Z",
     "iopub.status.idle": "2022-02-06T09:11:30.682875Z",
     "shell.execute_reply": "2022-02-06T09:11:30.682137Z",
     "shell.execute_reply.started": "2022-02-06T09:11:30.677294Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:\n",
      "[[9 7 9 5]\n",
      " [2 6 8 7]\n",
      " [6 6 4 7]]\n",
      "====================\n",
      "a1.reshape(2,6):\n",
      "[[9 7 9 5 2 6]\n",
      " [8 7 6 6 4 7]]\n",
      "====================\n",
      "a1:\n",
      "[[9 7 9 5]\n",
      " [2 6 8 7]\n",
      " [6 6 4 7]]\n"
     ]
    }
   ],
   "source": [
    "# reshape操作不会改变原数组\n",
    "a1 = np.random.randint(0, 10, size=(3, 4)) # 创建3行4列的数组a1，其中元素为0到9之间的随机整数\n",
    "print(\"a1:\\n{}\".format(a1))\n",
    "print(\"=\" * 20)\n",
    "print(\"a1.reshape(2,6):\\n{}\".format(a1.reshape(2,6))) # 对a1进行reshape操作，返回新shape的数组\n",
    "print(\"=\" * 20)\n",
    "print(\"a1:\\n{}\".format(a1)) # reshape操作不会改变原数组a1的shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "81c6a92b-5234-47c0-a07e-23f199fc3679",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-06T09:14:49.054165Z",
     "iopub.status.busy": "2022-02-06T09:14:49.053896Z",
     "iopub.status.idle": "2022-02-06T09:14:49.059588Z",
     "shell.execute_reply": "2022-02-06T09:14:49.058867Z",
     "shell.execute_reply.started": "2022-02-06T09:14:49.054139Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a2:\n",
      "[[3 0 9 6]\n",
      " [1 2 9 6]\n",
      " [6 7 0 7]]\n",
      "====================\n",
      "====================\n",
      "a2:\n",
      "[[3 0 9 6 1 2]\n",
      " [9 6 6 7 0 7]]\n"
     ]
    }
   ],
   "source": [
    "# resize操作会改变原数组\n",
    "a2 = np.random.randint(0, 10, size=(3, 4)) # 创建3行4列的数组a2，其中元素为0到9之间的随机整数\n",
    "print(\"a2:\\n{}\".format(a2))\n",
    "print(\"=\" * 20)\n",
    "a2.resize(2,6) # 对a2进行resize操作，没有返回值\n",
    "print(\"=\" * 20)\n",
    "print(\"a2:\\n{}\".format(a2)) # reshape操作改变了原数组a2的shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9d751222-008b-464b-96d5-5ca5186b4795",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-06T09:23:19.687806Z",
     "iopub.status.busy": "2022-02-06T09:23:19.687546Z",
     "iopub.status.idle": "2022-02-06T09:23:19.694564Z",
     "shell.execute_reply": "2022-02-06T09:23:19.693800Z",
     "shell.execute_reply.started": "2022-02-06T09:23:19.687779Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a3:\n",
      "[[9 9 0 2]\n",
      " [9 9 9 8]\n",
      " [6 4 4 6]]\n",
      "====================\n",
      "a4:\n",
      "[9 9 0 2 9 9 9 8 6 4 4 6]\n",
      "====================\n",
      "a4:\n",
      "[100   9   0   2   9   9   9   8   6   4   4   6]\n",
      "====================\n",
      "a3:\n",
      "[[9 9 0 2]\n",
      " [9 9 9 8]\n",
      " [6 4 4 6]]\n"
     ]
    }
   ],
   "source": [
    "# flatten操作不会改变原数组，仅返回一份原数组的一维copy\n",
    "a3 = np.random.randint(0, 10, size=(3, 4)) # 创建3行4列的数组a3，其中元素为0到9之间的随机整数\n",
    "print(\"a3:\\n{}\".format(a3))\n",
    "print(\"=\" * 20)\n",
    "a4=a3.flatten() # 对a3进行flatten操作，返回值赋给a4\n",
    "print(\"a4:\\n{}\".format(a4))\n",
    "print(\"=\" * 20)\n",
    "a4[0] = 100 # 修改a4第0个位置的值\n",
    "print(\"a4:\\n{}\".format(a4))\n",
    "print(\"=\" * 20)\n",
    "print(\"a3:\\n{}\".format(a3)) # 原数组没有变化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "81d9ec86-4d14-4438-8263-ebd442b40184",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-06T09:24:30.045136Z",
     "iopub.status.busy": "2022-02-06T09:24:30.044890Z",
     "iopub.status.idle": "2022-02-06T09:24:30.052290Z",
     "shell.execute_reply": "2022-02-06T09:24:30.051580Z",
     "shell.execute_reply.started": "2022-02-06T09:24:30.045110Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a5:\n",
      "[[4 4 4 1]\n",
      " [8 6 9 3]\n",
      " [3 7 9 0]]\n",
      "====================\n",
      "a6:\n",
      "[4 4 4 1 8 6 9 3 3 7 9 0]\n",
      "====================\n",
      "a6:\n",
      "[100   4   4   1   8   6   9   3   3   7   9   0]\n",
      "====================\n",
      "a5:\n",
      "[[100   4   4   1]\n",
      " [  8   6   9   3]\n",
      " [  3   7   9   0]]\n"
     ]
    }
   ],
   "source": [
    "# ravel操作不会改变原数组，返回一份原数组的一维view\n",
    "a5 = np.random.randint(0, 10, size=(3, 4)) # 创建3行4列的数组a5，其中元素为0到9之间的随机整数\n",
    "print(\"a5:\\n{}\".format(a5))\n",
    "print(\"=\" * 20)\n",
    "a6=a5.ravel() # 对a5进行ravel操作，返回值赋给a6\n",
    "print(\"a6:\\n{}\".format(a6))\n",
    "print(\"=\" * 20)\n",
    "a6[0] = 100 # 修改a6第0个位置的值\n",
    "print(\"a6:\\n{}\".format(a6))\n",
    "print(\"=\" * 20)\n",
    "print(\"a5:\\n{}\".format(a5)) # 原数组改变了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "315dba1d-5aed-426b-a8c4-eba9d0a341fe",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
